/**
 * 
 */
package org.mspring.mlog.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.mspring.mlog.entity.Attachment;
import org.mspring.mlog.entity.News;
import org.mspring.mlog.service.AttachmentService;
import org.mspring.mlog.service.NewsService;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.cache.service.DefaultCacheService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.mspring.platform.dao.hibernate.common.EnhancedRule;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013年6月28日
 */
@Service
@Transactional
public class NewsServiceImpl extends GenericDao<News, Long> implements NewsService {

    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private DefaultCacheService defaultCacheService;

    @Override
    public News save(News news) {
        // TODO Auto-generated method stub
        if (news.getCreateTime() == null) {
            news.setCreateTime(new Date());
        }
        if (news.getAuthor() == null) {
            news.setAuthor(SecurityUtils.getCurrentUser());
        }
        if (StringUtils.isBlank(news.getStatus())) {
            news.setStatus(News.Status.PUBLISH);
        }
        news = super.save(news);
        return news;
    }

    @Override
    public News merge(News news) {
        // TODO Auto-generated method stub
        if (news.getCreateTime() == null) {
            news.setCreateTime(new Date());
        }
        if (news.getAuthor() == null) {
            news.setAuthor(SecurityUtils.getCurrentUser());
        }
        if (StringUtils.isBlank(news.getStatus())) {
            news.setStatus(News.Status.PUBLISH);
        }
        return super.merge(news);
    }

    @Override
    public void update(News news) {
        // TODO Auto-generated method stub
        if (StringUtils.isBlank(news.getStatus())) {
            news.setStatus(News.Status.PUBLISH);
        }
        super.update(news);
    }

    @Override
    public void remove(News news) {
        // TODO Auto-generated method stub
        if (news == null || news.getId() == null) {
            return;
        }
        remove(news.getId());
    }

    @Override
    public void remove(Long... ids) {
        // TODO Auto-generated method stub
        for (Long id : ids) {
            if (id == null) {
                return;
            }

            // 删除对应的附件
            List<Attachment> attachments = attachmentService.findAttachments(id.toString(), Attachment.AttachFromType.FROM_NEWS);
            for (Attachment attachment : attachments) {
                attachmentService.deleteAttachment(attachment.getId());
            }
            super.remove(id);
        }
    }

    @Override
    public List<News> recent(int num) {
        // TODO Auto-generated method stub
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("createTime"));
        rule.add(Restrictions.eq("status", News.Status.PUBLISH));
        rule.setPageSize(num);
        return list(rule);
    }

    @Override
    public List<News> hot(int num, Date start, Date end) {
        // TODO Auto-generated method stub
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("views"));
        rule.add(Restrictions.eq("status", News.Status.PUBLISH));
        rule.setPageSize(num);
        if (start != null) {
            rule.add(Restrictions.ge("createTime", start));
        }
        if (end != null) {
            rule.add(Restrictions.le("createTime", end));
        }
        return list(rule);
    }
}
